<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>仿京东显示隐藏密码明文案例</title>
    <style>
        .box{
            width: 400px;
            border-bottom: 1px solid #ccc;
            margin: 100px auto;
            position: relative;
        }
        .box input{
            width: 370px;
            height: 30px;
            border: 0;
            outline: none;
        }
        .box img{
            width: 24px;
            position: absolute;
            top: 2px;
            right: 2px;
        }
    </style>
</head>
<body>
    <div class="box">
        <label for="">
            <img id="eye" src="source/close.png" alt="">
        </label>
        <input id="pw" type="password">
    </div>
    <script>
        //1.获取元素
        var eye = document.querySelector("#eye");
        var pw = document.querySelector('#pw');
        //2.注册事件 处理程序
        eye.onclick = function(){
            if(pw.type == 'text'){
                pw.type = 'password';
                eye.src = 'source/close.png';
            }else{
                pw.type = 'text';
                eye.src = 'source/open.png';
            }
        }
    </script>
</body>
</html>